package com.greenhouse.service.impl;

import com.greenhouse.config.Request;
import com.greenhouse.mapper.environment.EnvironmentMapper;
import com.greenhouse.mapper.environment.EquipmentMapper;
import com.greenhouse.mapper.greenhouse.GreenhouseMapper;
import com.greenhouse.model.Greenhouse;
import com.greenhouse.service.GreenhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GreenhouseServiceImpl implements GreenhouseService {
    @Autowired
    GreenhouseMapper greenhouseMapper;
    @Autowired
    EnvironmentMapper environmentMapper;
    @Autowired
    EquipmentMapper equipmentMapper;

    @Override
    public Request<Greenhouse> addGreenhouse(Integer userId, String name, String location, Integer area) {
        try {
            List<Greenhouse> greenhouses = greenhouseMapper.showGreenhousesByUserId(userId);
            if (greenhouses.size() > 0) {
                for (Greenhouse g : greenhouses) {
                    if (g.getName().equals(name)) {
                        return Request.error("创建失败，已有同名大棚", null);
                    }
                }
            }
            greenhouseMapper.insertGreenhouse(userId, name, location, area);
            // 添加大棚成功，创建环境信息表
            Greenhouse greenhouse = greenhouseMapper.getGreenhouse(name, userId);
            String tableName = "e_" + userId + "_" + greenhouse.getId();
            environmentMapper.createGreanhouserEnvironment(tableName);
            System.out.println("创建环境表成功");
            //添加基本设备信息（温度，湿度，关照，土壤，CO2，PH）
            String tabletName = "e_" + userId;
            equipmentMapper.insertDataToExist(tabletName,greenhouse.getId(), "温度传感器");
            equipmentMapper.insertDataToExist(tabletName,greenhouse.getId(), "湿度传感器");
            equipmentMapper.insertDataToExist(tabletName,greenhouse.getId(), "光照传感器");
            equipmentMapper.insertDataToExist(tabletName,greenhouse.getId(), "土壤传感器");
            equipmentMapper.insertDataToExist(tabletName,greenhouse.getId(), "CO2传感器");
            equipmentMapper.insertDataToExist(tabletName,greenhouse.getId(), "PH传感器");
            return Request.success("创建大棚成功", greenhouse);
        } catch (Exception e) {
            // 记录日志，处理异常
            return Request.error(e.getMessage(),null);
        }
    }

    @Override
    public Request<Greenhouse> updateGreenhouse(Greenhouse greenhouse) {
        System.out.println(greenhouse);
        int n = greenhouseMapper.updateGreenhouses(greenhouse);
        if (n == 1){
            return Request.success("修改成功", greenhouse);
        }
        return Request.error("修改失败", null);
    }

    @Override
    public Request<String> deleteGreenhouse(Integer userId, Integer id) {
        //删除大棚
        Integer n = greenhouseMapper.deleteGreenhouse(id);
        if (n == 1){
            //删除大棚环境表
            n = equipmentMapper.deleteUserEquipment("e_"+ userId,id);
            if (n > 0){
                n = environmentMapper.deleteUserEquipment("e_"+ userId +"_" + id);
            }
        }
        return null;
    }

    @Override
    public List<Greenhouse> selectAll(Integer id) {
        List<Greenhouse> greenhouses = greenhouseMapper.showGreenhousesByUserId(id);
        return greenhouses;
    }
    @Override
    public Request<String> updateGreenhouseStatus(Integer id, Integer status){
        Integer n = greenhouseMapper.updateGreenhouseStatus(id, status);
        if (n == 1){
            return Request.success("修改状态成功", null);
        }
        return Request.error("修改状态失败", null);
    }
}
